import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import './api/mock'
import exportExcel from './utils/excel'
import VueSmoothScroll from 'vue2-smooth-scroll'
import * as echarts from 'echarts'
import moment from 'moment'
import LoadingPage from './components/LoadingPage.vue'
import evUrl from './utils/everyUrl'

Vue.component('LoadingPage', LoadingPage)
Vue.prototype.$moment = moment
Vue.prototype.$echarts = echarts
Vue.prototype.$evUrl = evUrl
Vue.use(VueSmoothScroll)
Vue.prototype.$excels = exportExcel
Vue.use(ElementUI)
Vue.config.productionTip = false
router.beforeEach((to, from, next) => {
  const token = window.localStorage.getItem('token')
  if (!token && (to.name === 'tRegister' || to.name === 'contact' || to.name === 'sRegister' || to.name === 'apply' ||
    to.name === 'home' || to.name === 'myShare' || to.name === 'bookDisplay')) {
    next()
  } else if (!token && to.name !== 'login') {
    next({ name: from.name })
  } else if (token && to.name === 'login') {
    next({ name: from.name })
  } else {
    next()
  }
})

new Vue({
  router,
  store,
  created() {
    store.commit('addMenu', router)
  },
  render: h => h(App)
}).$mount('#app')
